package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 菜品控制器
 */

@RestController
@Api(tags = "菜品功能")
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {

    @Resource
    private DishService dishService;

    @ApiOperation("新增菜品")
    @PostMapping
    public Result<String> addDish(@RequestBody DishDTO dishDTO) {
        log.info("【新增菜品】 dish：{}",dishDTO);
        Integer tag = dishService.addDish(dishDTO);
        if (tag > 0) {
            return Result.success("新增成功");
        }else  {
            return Result.error("新增失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation("批量删除菜品")
    @DeleteMapping
    public Result<String> removeBatch(@RequestParam List<Long> ids) {
        log.info("【批量删除菜品】 ids：{}",ids);
        Integer tag = dishService.batchRemove(ids);
        return Result.success(tag > 0?"删除成功":"删除失败");
    }

    /**
     * 修改菜品信息
     * @param dishDTO
     * @return
     */
    @PutMapping
    public Result<String> updateById(@RequestBody DishDTO dishDTO) {
        log.info("【更新菜品信息】dishDTO:{}",dishDTO);
        Integer tag = dishService.udpateById(dishDTO);
        if (tag > 0) {
            return Result.success("更新成功！");
        }else  {
            return Result.error("更新失败！");
        }
    }

    /**
     * 菜品的分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> findByPage(DishPageQueryDTO dishPageQueryDTO) {
        log.info("【菜品分页查询】dto:{}",dishPageQueryDTO);
        PageResult pageResult =  dishService.findByPage(dishPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<DishVO> findById(@PathVariable Long id) {
        log.info("【根据id查询菜品】id:{}",id);
        DishVO dishVO = dishService.findById(id);
        if (dishVO != null) {
            return Result.success(dishVO);
        }else  {
            return Result.error("查询失败");
        }
    }


    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public Result findByCategoryId(Long categoryId) {
        log.info("【根据分类id查询】 categoryId:{}",categoryId);
        List<DishVO> dishVOS = dishService.findByCategoryId(categoryId);
        return Result.success(dishVOS);
    }
}
